package org.apoiolivre.ui.android;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;


import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.*;

public class MainActivity extends ListActivity {
	
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	
    	super.onCreate(savedInstanceState);
        setContentView(R.layout.main);   
        
        String[] movimentos = runUrl();
        setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, movimentos));
        
    }
	
	 private String[]  runUrl() {
		List<String> movimentos = new ArrayList<String>();
        HttpClient mClient= new DefaultHttpClient();
        HttpGet get = new HttpGet("http://apoiolivreapp.appspot.com/data/BuscaMovimento");

		try {
			String resposta =  HttpHelper.request(mClient.execute(get)); 
			
			Type collectionType = new TypeToken<List<Movimento>>(){}.getType();
			List<Movimento> lst= new Gson().fromJson(resposta, collectionType);
			
			 for(Movimento mov: lst) {
				 movimentos.add(mov.getDescription()+" - "+mov.getValue());
			 }
		} catch (Exception e) {
			e.printStackTrace();
		} 
        
        
        return (String[]) movimentos.toArray(new String[0]);

	 } // End runurl()
   
}